Add recognition code for the SiS 645DX.
[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.8 2004/01/23 14:11: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, 0x07501039, 0)) {      /* SiS 750 */
527             int8_t reg = 0x40 + (devno << 1);
528             int16_t val = pci_read_config(parent, reg, 2) & 0x0fff;
529
530             if (udmamode >= 5) {
531                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
532                                     ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
533                 if (bootverbose)
534                     ata_prtdev(atadev, "%s setting UDMA5 on SiS chip\n",
535                                (error) ? "failed" : "success");
536                 if (!error) {
537                     pci_write_config(parent, reg, val | 0x8000, 2);
538                     atadev->mode = ATA_UDMA5;
539                     return;
540                 }
541             }
542             if (udmamode >= 4) {
543                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
544                                     ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
545                 if (bootverbose)
546                     ata_prtdev(atadev, "%s setting UDMA4 on SiS chip\n",
547                                (error) ? "failed" : "success");
548                 if (!error) {
549                     pci_write_config(parent, reg, val | 0x9000, 2);
550                     atadev->mode = ATA_UDMA4;
551                     return;
552                 }
553             }
554             if (udmamode >= 2) {
555                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
556                                     ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
557                 if (bootverbose)
558                     ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
559                                (error) ? "failed" : "success");
560                 if (!error) {
561                     pci_write_config(parent, reg, val | 0xb000, 2);
562                     atadev->mode = ATA_UDMA2;
563                     return;
564                 }
565             }
566         } else if (ata_find_dev(parent, 0x05301039, 0) || /* SiS 530 */
567                    ata_find_dev(parent, 0x05401039, 0) || /* SiS 540 */
568                    ata_find_dev(parent, 0x06201039, 0) || /* SiS 620 */
569                    ata_find_dev(parent, 0x06301039, 0)) { /* SiS 630 */
570             int8_t reg = 0x40 + (devno << 1);
571             int16_t val = pci_read_config(parent, reg, 2) & 0x0fff;
572
573             if (udmamode >= 4) {
574                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
575                                     ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
576                 if (bootverbose)
577                     ata_prtdev(atadev, "%s setting UDMA4 on SiS chip\n",
578                                (error) ? "failed" : "success");
579                 if (!error) {
580                     pci_write_config(parent, reg, val | 0x9000, 2);
581                     atadev->mode = ATA_UDMA4;
582                     return;
583                 }
584             }
585             if (udmamode >= 2) {
586                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
587                                     ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
588                 if (bootverbose)
589                     ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
590                                (error) ? "failed" : "success");
591                 if (!error) {
592                     pci_write_config(parent, reg, val | 0xa000, 2);
593                     atadev->mode = ATA_UDMA2;
594                     return;
595                 }
596             }
597         } else if (udmamode >= 2 && pci_get_revid(parent) > 0xc1) {
598             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
599                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
600             if (bootverbose)
601                 ata_prtdev(atadev, "%s setting UDMA2 on SiS chip\n",
602                            (error) ? "failed" : "success");
603             if (!error) {
604                 pci_write_config(parent, 0x40 + (devno << 1), 0xa301, 2);
605                 atadev->mode = ATA_UDMA2;
606                 return;
607             }
608         }
609         if (wdmamode >=2 && apiomode >= 4) {
610             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
611                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
612             if (bootverbose)
613                 ata_prtdev(atadev, "%s setting WDMA2 on SiS chip\n",
614                            (error) ? "failed" : "success");
615             if (!error) {
616                 pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
617                 atadev->mode = ATA_WDMA2;
618                 return;
619             }
620         }
621         /* we could set PIO mode timings, but we assume the BIOS did that */
622         break;
623
624     case 0x06801095:    /* SiI 0680 ATA133 controller */
625         {
626             u_int8_t ureg = 0xac + (ATA_DEV(device) * 0x02) + (ch->unit * 0x10);
627             u_int8_t uval = pci_read_config(parent, ureg, 1);
628             u_int8_t mreg = ch->unit ? 0x84 : 0x80;
629             u_int8_t mask = ATA_DEV(device) ? 0x30 : 0x03;
630             u_int8_t mode = pci_read_config(parent, mreg, 1);
631
632             /* enable UDMA mode */
633             pci_write_config(parent, mreg,
634                              (mode & ~mask) | (device ? 0x30 : 0x03), 1);
635             if (udmamode >= 6) {
636                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
637                                     ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
638                 if (bootverbose)
639                     ata_prtdev(atadev, "%s setting UDMA6 on SiI chip\n",
640                                (error) ? "failed" : "success");
641                 if (!error) {
642                     pci_write_config(parent, ureg, (uval & 0x3f) | 0x01, 1);
643                     atadev->mode = ATA_UDMA6;
644                     return;
645                 }
646             }
647             if (udmamode >= 5) {
648                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
649                                     ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
650                 if (bootverbose)
651                     ata_prtdev(atadev, "%s setting UDMA5 on SiI chip\n",
652                                (error) ? "failed" : "success");
653                 if (!error) {
654                     pci_write_config(parent, ureg, (uval & 0x3f) | 0x02, 1);
655                     atadev->mode = ATA_UDMA5;
656                     return;
657                 }
658             }
659             if (udmamode >= 4) {
660                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
661                                     ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
662                 if (bootverbose)
663                     ata_prtdev(atadev, "%s setting UDMA4 on SiI chip\n",
664                                (error) ? "failed" : "success");
665                 if (!error) {
666                     pci_write_config(parent, ureg, (uval & 0x3f) | 0x03, 1);
667                     atadev->mode = ATA_UDMA4;
668                     return;
669                 }
670             }
671             if (udmamode >= 2) {
672                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
673                                     ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
674                 if (bootverbose)
675                     ata_prtdev(atadev, "%s setting UDMA2 on SiI chip\n",
676                                (error) ? "failed" : "success");
677                 if (!error) {
678                     pci_write_config(parent, ureg, (uval & 0x3f) | 0x07, 1);
679                     atadev->mode = ATA_UDMA2;
680                     return;
681                 }
682             }
683
684             /* disable UDMA mode and enable WDMA mode */
685             pci_write_config(parent, mreg,
686                              (mode & ~mask) | (device ? 0x20 : 0x02), 1);
687             if (wdmamode >= 2 && apiomode >= 4) {
688                 error = ata_command(atadev, ATA_C_SETFEATURES, 0,
689                                     ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
690                 if (bootverbose)
691                     ata_prtdev(atadev, "%s setting WDMA2 on SiI chip\n",
692                                (error) ? "failed" : "success");
693                 if (!error) {
694                     pci_write_config(parent, ureg - 0x4, 0x10c1, 2);
695                     atadev->mode = ATA_WDMA2;
696                     return;
697                 }
698             }
699
700             /* restore PIO mode */
701             pci_write_config(parent, mreg, mode, 1);
702         }
703         /* we could set PIO mode timings, but we assume the BIOS did that */
704         break;
705
706
707     case 0x06491095:    /* CMD 649 ATA100 controller */
708         if (udmamode >= 5) {
709             u_int8_t umode;
710
711             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
712                                 ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
713             if (bootverbose)
714                 ata_prtdev(atadev, "%s setting UDMA5 on CMD chip\n",
715                            (error) ? "failed" : "success");
716             if (!error) {
717                 umode = pci_read_config(parent, ch->unit ? 0x7b : 0x73, 1);
718                 umode &= ~(device == ATA_MASTER ? 0x35 : 0xca);
719                 umode |= (device == ATA_MASTER ? 0x05 : 0x0a);
720                 pci_write_config(parent, ch->unit ? 0x7b : 0x73, umode, 1);
721                 atadev->mode = ATA_UDMA5;
722                 return;
723             }
724         }
725         /* FALLTHROUGH */
726
727     case 0x06481095:    /* CMD 648 ATA66 controller */
728         if (udmamode >= 4) {
729             u_int8_t umode;
730
731             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
732                                 ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
733             if (bootverbose)
734                 ata_prtdev(atadev, "%s setting UDMA4 on CMD chip\n",
735                            (error) ? "failed" : "success");
736             if (!error) {
737                 umode = pci_read_config(parent, ch->unit ? 0x7b : 0x73, 1);
738                 umode &= ~(device == ATA_MASTER ? 0x35 : 0xca);
739                 umode |= (device == ATA_MASTER ? 0x15 : 0x4a);
740                 pci_write_config(parent, ch->unit ? 0x7b : 0x73, umode, 1);
741                 atadev->mode = ATA_UDMA4;
742                 return;
743             }
744         }
745         if (udmamode >= 2) {
746             u_int8_t umode;
747
748             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
749                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
750             if (bootverbose)
751                 ata_prtdev(atadev, "%s setting UDMA2 on CMD chip\n",
752                            (error) ? "failed" : "success");
753             if (!error) {
754                 umode = pci_read_config(parent, ch->unit ? 0x7b : 0x73, 1);
755                 umode &= ~(device == ATA_MASTER ? 0x35 : 0xca);
756                 umode |= (device == ATA_MASTER ? 0x11 : 0x42);
757                 pci_write_config(parent, ch->unit ? 0x7b : 0x73, umode, 1);
758                 atadev->mode = ATA_UDMA2;
759                 return;
760             }
761         }
762         /* make sure eventual UDMA mode from the BIOS is disabled */
763         pci_write_config(parent, ch->unit ? 0x7b : 0x73, 
764                          pci_read_config(parent, ch->unit ? 0x7b : 0x73, 1)&
765                          ~(device == ATA_MASTER ? 0x35 : 0xca), 1);
766         /* FALLTHROUGH */
767
768     case 0x06461095:    /* CMD 646 ATA controller */
769         if (wdmamode >= 2 && apiomode >= 4) {
770             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
771                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
772             if (bootverbose)
773                 ata_prtdev(atadev, "%s setting WDMA2 on CMD chip\n",
774                            error ? "failed" : "success");
775             if (!error) {
776                 int32_t offset = (devno < 3) ? (devno << 1) : 7;
777
778                 pci_write_config(parent, 0x54 + offset, 0x3f, 1);
779                 atadev->mode = ATA_WDMA2;
780                 return;
781             }
782         }
783         /* we could set PIO mode timings, but we assume the BIOS did that */
784         break;
785
786     case 0xc6931080:    /* Cypress 82c693 ATA controller */
787         if (wdmamode >= 2 && apiomode >= 4) {
788             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
789                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
790             if (bootverbose)
791                 ata_prtdev(atadev, "%s setting WDMA2 on Cypress chip\n",
792                            error ? "failed" : "success");
793             if (!error) {
794                 pci_write_config(ch->dev, ch->unit ? 0x4e:0x4c, 0x2020, 2);
795                 atadev->mode = ATA_WDMA2;
796                 return;
797             }
798         }
799         /* we could set PIO mode timings, but we assume the BIOS did that */
800         break;
801
802     case 0x01021078:    /* Cyrix 5530 ATA33 controller */
803         ch->alignment = 0xf;    /* DMA engine requires 16 byte alignment */
804         if (udmamode >= 2) {
805             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
806                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
807             if (bootverbose)
808                 ata_prtdev(atadev, "%s setting UDMA2 on Cyrix chip\n",
809                            (error) ? "failed" : "success");
810             if (!error) {
811                 cyrix_timing(ch, devno, ATA_UDMA2);
812                 atadev->mode = ATA_UDMA2;
813                 return;
814             }
815         }
816         if (wdmamode >= 2 && apiomode >= 4) {
817             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
818                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
819             if (bootverbose)
820                 ata_prtdev(atadev, "%s setting WDMA2 on Cyrix chip\n",
821                            (error) ? "failed" : "success");
822             if (!error) {
823                 cyrix_timing(ch, devno, ATA_WDMA2);
824                 atadev->mode = ATA_WDMA2;
825                 return;
826             }
827         }
828         error = ata_command(atadev, ATA_C_SETFEATURES, 0,
829                             ATA_PIO0 + apiomode, ATA_C_F_SETXFER,
830                             ATA_WAIT_READY);
831         if (bootverbose)
832             ata_prtdev(atadev, "%s setting %s on Cyrix chip\n",
833                        (error) ? "failed" : "success",
834                        ata_mode2str(ATA_PIO0 + apiomode));
835         cyrix_timing(ch, devno, ATA_PIO0 + apiomode);
836         atadev->mode = ATA_PIO0 + apiomode;
837         return;
838
839     case 0x02121166:    /* ServerWorks CSB5 ATA66/100 controller */
840         if (udmamode >= 5 && pci_get_revid(parent) >= 0x92) {
841             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
842                                 ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
843             if (bootverbose)
844                 ata_prtdev(atadev, "%s setting UDMA5 on ServerWorks chip\n",
845                            (error) ? "failed" : "success");
846             if (!error) {
847                 u_int16_t reg56;
848
849                 pci_write_config(parent, 0x54, 
850                                  pci_read_config(parent, 0x54, 1) |
851                                  (0x01 << devno), 1);
852                 reg56 = pci_read_config(parent, 0x56, 2);
853                 reg56 &= ~(0xf << (devno * 4));
854                 reg56 |= (0x5 << (devno * 4));
855                 pci_write_config(parent, 0x56, reg56, 2);
856                 atadev->mode = ATA_UDMA5;
857                 return;
858             }
859         }
860         if (udmamode >= 4) {
861             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
862                                 ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
863             if (bootverbose)
864                 ata_prtdev(atadev, "%s setting UDMA4 on ServerWorks chip\n",
865                            (error) ? "failed" : "success");
866             if (!error) {
867                 u_int16_t reg56;
868
869                 pci_write_config(parent, 0x54, 
870                                  pci_read_config(parent, 0x54, 1) |
871                                  (0x01 << devno), 1);
872                 reg56 = pci_read_config(parent, 0x56, 2);
873                 reg56 &= ~(0xf << (devno * 4));
874                 reg56 |= (0x4 << (devno * 4));
875                 pci_write_config(parent, 0x56, reg56, 2);
876                 atadev->mode = ATA_UDMA4;
877                 return;
878             }
879         }
880         /* FALLTHROUGH */
881
882     case 0x02111166:    /* ServerWorks ROSB4 ATA33 controller */
883         if (udmamode >= 2) {
884             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
885                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
886             if (bootverbose)
887                 ata_prtdev(atadev, "%s setting UDMA2 on ServerWorks chip\n",
888                            (error) ? "failed" : "success");
889             if (!error) {
890                 u_int16_t reg56;
891
892                 pci_write_config(parent, 0x54, 
893                                  pci_read_config(parent, 0x54, 1) |
894                                  (0x01 << devno), 1);
895                 reg56 = pci_read_config(parent, 0x56, 2);
896                 reg56 &= ~(0xf << (devno * 4));
897                 reg56 |= (0x2 << (devno * 4));
898                 pci_write_config(parent, 0x56, reg56, 2);
899                 atadev->mode = ATA_UDMA2;
900                 return;
901             }
902         }
903         if (wdmamode >= 2 && apiomode >= 4) {
904             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
905                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
906             if (bootverbose)
907                 ata_prtdev(atadev, "%s setting WDMA2 on ServerWorks chip\n",
908                            (error) ? "failed" : "success");
909             if (!error) {
910                 int offset = (ch->unit * 2) + (device == ATA_MASTER);
911                 int word44 = pci_read_config(parent, 0x44, 4);
912
913                 pci_write_config(parent, 0x54,
914                                  pci_read_config(parent, 0x54, 1) &
915                                  ~(0x01 << devno), 1);
916                 word44 &= ~(0xff << (offset << 8));
917                 word44 |= (0x20 << (offset << 8));
918                 pci_write_config(parent, 0x44, 0x20, 4);
919                 atadev->mode = ATA_WDMA2;
920                 return;
921             }
922         }
923         /* we could set PIO mode timings, but we assume the BIOS did that */
924         break;
925
926     case 0x4d69105a:    /* Promise TX2 ATA133 controllers */
927     case 0x5275105a:    /* Promise TX2 ATA133 controllers */
928     case 0x6269105a:    /* Promise TX2 ATA133 controllers */
929     case 0x7275105a:    /* Promise TX2 ATA133 controllers */
930         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
931         if (udmamode >= 6 && !(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
932             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
933                                 ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
934             if (bootverbose)
935                 ata_prtdev(atadev, "%s setting UDMA6 on Promise chip\n",
936                            (error) ? "failed" : "success");
937             if (!error) {
938                 atadev->mode = ATA_UDMA6;
939                 return;
940             }
941         }
942         /* FALLTHROUGH */
943
944     case 0x4d68105a:    /* Promise TX2 ATA100 controllers */
945     case 0x6268105a:    /* Promise TX2 ATA100 controllers */
946         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
947         if (udmamode >= 5 && !(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
948             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
949                                 ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
950             if (bootverbose)
951                 ata_prtdev(atadev, "%s setting UDMA5 on Promise chip\n",
952                            (error) ? "failed" : "success");
953             if (!error) {
954                 atadev->mode = ATA_UDMA5;
955                 return;
956             }
957         }
958         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
959         if (udmamode >= 4 && !(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x04)) {
960             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
961                                 ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
962             if (bootverbose)
963                 ata_prtdev(atadev, "%s setting UDMA4 on Promise chip\n",
964                            (error) ? "failed" : "success");
965             if (!error) {
966                 atadev->mode = ATA_UDMA4;
967                 return;
968             }
969         }
970         if (udmamode >= 2) {
971             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
972                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
973             if (bootverbose)
974                 ata_prtdev(atadev, "%s setting UDMA on Promise chip\n",
975                            (error) ? "failed" : "success");
976             if (!error) {
977                 atadev->mode = ATA_UDMA2;
978                 return;
979             }
980         }
981         if (wdmamode >= 2 && apiomode >= 4) {
982             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
983                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
984             if (bootverbose)
985                 ata_prtdev(atadev, "%s setting WDMA2 on Promise chip\n",
986                            (error) ? "failed" : "success");
987             if (!error) {
988                 atadev->mode = ATA_WDMA2;
989                 return;
990             }
991         }
992         break;
993
994     case 0x0d30105a:    /* Promise OEM ATA100 controllers */
995     case 0x4d30105a:    /* Promise Ultra/FastTrak 100 controllers */
996         if (!ATAPI_DEVICE(ch, device) && udmamode >= 5 && 
997             !(pci_read_config(parent, 0x50, 2)&(ch->unit ? 1<<11 : 1<<10))){
998             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
999                                 ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
1000             if (bootverbose)
1001                 ata_prtdev(atadev, "%s setting UDMA5 on Promise chip\n",
1002                            (error) ? "failed" : "success");
1003             if (!error) {
1004                 promise_timing(ch, devno, ATA_UDMA5);
1005                 atadev->mode = ATA_UDMA5;
1006                 return;
1007             }
1008         }
1009         /* FALLTHROUGH */
1010
1011     case 0x0d38105a:    /* Promise FastTrak 66 controllers */
1012     case 0x4d38105a:    /* Promise Ultra/FastTrak 66 controllers */
1013         if (!ATAPI_DEVICE(ch, device) && udmamode >= 4 && 
1014             !(pci_read_config(parent, 0x50, 2)&(ch->unit ? 1<<11 : 1<<10))){
1015             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1016                                 ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
1017             if (bootverbose)
1018                 ata_prtdev(atadev, "%s setting UDMA4 on Promise chip\n",
1019                            (error) ? "failed" : "success");
1020             if (!error) {
1021                 promise_timing(ch, devno, ATA_UDMA4);
1022                 atadev->mode = ATA_UDMA4;
1023                 return;
1024             }
1025         }
1026         /* FALLTHROUGH */
1027
1028     case 0x4d33105a:    /* Promise Ultra/FastTrak 33 controllers */
1029         if (!ATAPI_DEVICE(ch, device) && udmamode >= 2) {
1030             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1031                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1032             if (bootverbose)
1033                 ata_prtdev(atadev, "%s setting UDMA2 on Promise chip\n",
1034                            (error) ? "failed" : "success");
1035             if (!error) {
1036                 promise_timing(ch, devno, ATA_UDMA2);
1037                 atadev->mode = ATA_UDMA2;
1038                 return;
1039             }
1040         }
1041         if (!ATAPI_DEVICE(ch, device) && wdmamode >= 2 && apiomode >= 4) {
1042             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1043                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1044             if (bootverbose)
1045                 ata_prtdev(atadev, "%s setting WDMA2 on Promise chip\n",
1046                            (error) ? "failed" : "success");
1047             if (!error) {
1048                 promise_timing(ch, devno, ATA_WDMA2);
1049                 atadev->mode = ATA_WDMA2;
1050                 return;
1051             }
1052         }
1053         error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1054                             ATA_PIO0 + apiomode, 
1055                             ATA_C_F_SETXFER, ATA_WAIT_READY);
1056         if (bootverbose)
1057             ata_prtdev(atadev, "%s setting PIO%d on Promise chip\n",
1058                        (error) ? "failed" : "success",
1059                        (apiomode >= 0) ? apiomode : 0);
1060         promise_timing(ch, devno, ATA_PIO0 + apiomode);
1061         atadev->mode = ATA_PIO0 + apiomode;
1062         return;
1063     
1064     case 0x00041103:    /* HighPoint HPT366/368/370/372 controllers */
1065     case 0x00051103:    /* HighPoint HPT372 controllers */
1066     case 0x00081103:    /* HighPoint HPT374 controllers */
1067         if (!ATAPI_DEVICE(ch, device) && udmamode >= 6 && hpt_cable80(ch) &&
1068             ((ch->chiptype == 0x00041103 && pci_get_revid(parent) >= 0x05) ||
1069              (ch->chiptype == 0x00051103 && pci_get_revid(parent) >= 0x01) ||
1070              (ch->chiptype == 0x00081103 && pci_get_revid(parent) >= 0x07))) {
1071             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1072                                 ATA_UDMA6, ATA_C_F_SETXFER, ATA_WAIT_READY);
1073             if (bootverbose)
1074                 ata_prtdev(atadev, "%s setting UDMA6 on HighPoint chip\n",
1075                            (error) ? "failed" : "success");
1076             if (!error) {
1077                 hpt_timing(ch, devno, ATA_UDMA6);
1078                 atadev->mode = ATA_UDMA6;
1079                 return;
1080             }
1081         }
1082         if (!ATAPI_DEVICE(ch, device) && udmamode >= 5 && hpt_cable80(ch) &&
1083             ((ch->chiptype == 0x00041103 && pci_get_revid(parent) >= 0x03) ||
1084              (ch->chiptype == 0x00051103 && pci_get_revid(parent) >= 0x01) ||
1085              (ch->chiptype == 0x00081103 && pci_get_revid(parent) >= 0x07))) {
1086             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1087                                 ATA_UDMA5, ATA_C_F_SETXFER, ATA_WAIT_READY);
1088             if (bootverbose)
1089                 ata_prtdev(atadev, "%s setting UDMA5 on HighPoint chip\n",
1090                            (error) ? "failed" : "success");
1091             if (!error) {
1092                 hpt_timing(ch, devno, ATA_UDMA5);
1093                 atadev->mode = ATA_UDMA5;
1094                 return;
1095             }
1096         }
1097         if (!ATAPI_DEVICE(ch, device) && udmamode >= 4 && hpt_cable80(ch)) {
1098             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1099                                 ATA_UDMA4, ATA_C_F_SETXFER, ATA_WAIT_READY);
1100             if (bootverbose)
1101                 ata_prtdev(atadev, "%s setting UDMA4 on HighPoint chip\n",
1102                            (error) ? "failed" : "success");
1103             if (!error) {
1104                 hpt_timing(ch, devno, ATA_UDMA4);
1105                 atadev->mode = ATA_UDMA4;
1106                 return;
1107             }
1108         }
1109         if (!ATAPI_DEVICE(ch, device) && udmamode >= 2) {
1110             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1111                                 ATA_UDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1112             if (bootverbose)
1113                 ata_prtdev(atadev, "%s setting UDMA2 on HighPoint chip\n",
1114                            (error) ? "failed" : "success");
1115             if (!error) {
1116                 hpt_timing(ch, devno, ATA_UDMA2);
1117                 atadev->mode = ATA_UDMA2;
1118                 return;
1119             }
1120         }
1121         if (!ATAPI_DEVICE(ch, device) && wdmamode >= 2 && apiomode >= 4) {
1122             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1123                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1124             if (bootverbose)
1125                 ata_prtdev(atadev, "%s setting WDMA2 on HighPoint chip\n",
1126                            (error) ? "failed" : "success");
1127             if (!error) {
1128                 hpt_timing(ch, devno, ATA_WDMA2);
1129                 atadev->mode = ATA_WDMA2;
1130                 return;
1131             }
1132         }
1133         error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1134                             ATA_PIO0 + apiomode, 
1135                             ATA_C_F_SETXFER, ATA_WAIT_READY);
1136         if (bootverbose)
1137             ata_prtdev(atadev, "%s setting PIO%d on HighPoint chip\n",
1138                        (error) ? "failed" : "success",
1139                        (apiomode >= 0) ? apiomode : 0);
1140         hpt_timing(ch, devno, ATA_PIO0 + apiomode);
1141         atadev->mode = ATA_PIO0 + apiomode;
1142         return;
1143
1144     case 0x000116ca:    /* Cenatek Rocket Drive controller */
1145         if (wdmamode >= 0 &&
1146             (ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) & 
1147              ((device==ATA_MASTER)?ATA_BMSTAT_DMA_MASTER:ATA_BMSTAT_DMA_SLAVE)))
1148             atadev->mode = ATA_DMA;
1149         else
1150             atadev->mode = ATA_PIO;
1151         return;
1152
1153     default:            /* unknown controller chip */
1154         /* better not try generic DMA on ATAPI devices it almost never works */
1155         if ((device == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
1156             (device == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE))
1157             break;
1158
1159         /* if controller says its setup for DMA take the easy way out */
1160         /* the downside is we dont know what DMA mode we are in */
1161         if ((udmamode >= 0 || wdmamode >= 2) &&
1162             (ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) &
1163              ((device==ATA_MASTER) ? 
1164               ATA_BMSTAT_DMA_MASTER : ATA_BMSTAT_DMA_SLAVE))) {
1165             atadev->mode = ATA_DMA;
1166             return;
1167         }
1168
1169         /* well, we have no support for this, but try anyways */
1170         if ((wdmamode >= 2 && apiomode >= 4) && ch->r_bmio) {
1171             error = ata_command(atadev, ATA_C_SETFEATURES, 0,
1172                                 ATA_WDMA2, ATA_C_F_SETXFER, ATA_WAIT_READY);
1173             if (bootverbose)
1174                 ata_prtdev(atadev, "%s setting WDMA2 on generic chip\n",
1175                            (error) ? "failed" : "success");
1176             if (!error) {
1177                 atadev->mode = ATA_WDMA2;
1178                 return;
1179             }
1180         }
1181     }
1182     error = ata_command(atadev, ATA_C_SETFEATURES, 0, ATA_PIO0 + apiomode,
1183                         ATA_C_F_SETXFER, ATA_WAIT_READY);
1184     if (bootverbose)
1185         ata_prtdev(atadev, "%s setting PIO%d on generic chip\n",
1186                    (error) ? "failed" : "success", apiomode < 0 ? 0 : apiomode);
1187     if (!error)
1188         atadev->mode = ATA_PIO0 + apiomode;
1189     else {
1190         if (bootverbose)
1191             ata_prtdev(atadev, "using PIO mode set by BIOS\n");
1192         atadev->mode = ATA_PIO;
1193     }
1194 }
1195
1196 int
1197 ata_dmasetup(struct ata_channel *ch, int device, struct ata_dmaentry *dmatab,
1198              caddr_t data, int32_t count)
1199 {
1200     u_int32_t dma_count, dma_base;
1201     int i = 0;
1202
1203     if (((uintptr_t)data & ch->alignment) || (count & ch->alignment)) {
1204         ata_printf(ch, device, "non aligned DMA transfer attempted\n");
1205         return -1;
1206     }
1207
1208     if (!count) {
1209         ata_printf(ch, device, "zero length DMA transfer attempted\n");
1210         return -1;
1211     }
1212     
1213     dma_base = vtophys(data);
1214     dma_count = imin(count, (PAGE_SIZE - ((uintptr_t)data & PAGE_MASK)));
1215     data += dma_count;
1216     count -= dma_count;
1217
1218     while (count) {
1219         dmatab[i].base = dma_base;
1220         dmatab[i].count = (dma_count & 0xffff);
1221         i++; 
1222         if (i >= ATA_DMA_ENTRIES) {
1223             ata_printf(ch, device, "too many segments in DMA table\n");
1224             return -1;
1225         }
1226         dma_base = vtophys(data);
1227         dma_count = imin(count, PAGE_SIZE);
1228         data += imin(count, PAGE_SIZE);
1229         count -= imin(count, PAGE_SIZE);
1230     }
1231     dmatab[i].base = dma_base;
1232     dmatab[i].count = (dma_count & 0xffff) | ATA_DMA_EOT;
1233     return 0;
1234 }
1235
1236 void
1237 ata_dmastart(struct ata_channel *ch, int device, 
1238              struct ata_dmaentry *dmatab, int dir)
1239 {
1240     ch->flags |= ATA_DMA_ACTIVE;
1241     ATA_OUTL(ch->r_bmio, ATA_BMDTP_PORT, vtophys(dmatab));
1242     ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT, dir ? ATA_BMCMD_WRITE_READ : 0);
1243     ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, 
1244          (ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) | 
1245           (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
1246     ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT, 
1247          ATA_INB(ch->r_bmio, ATA_BMCMD_PORT) | ATA_BMCMD_START_STOP);
1248 }
1249
1250 int
1251 ata_dmadone(struct ata_channel *ch)
1252 {
1253     int error;
1254
1255     ATA_OUTB(ch->r_bmio, ATA_BMCMD_PORT, 
1256                 ATA_INB(ch->r_bmio, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
1257     ch->flags &= ~ATA_DMA_ACTIVE;
1258     error = ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT);
1259     ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, 
1260              error | ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
1261     return error & ATA_BMSTAT_MASK;
1262 }
1263
1264 int
1265 ata_dmastatus(struct ata_channel *ch)
1266 {
1267     return ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1268 }
1269
1270 static void
1271 cyrix_timing(struct ata_channel *ch, int devno, int mode)
1272 {
1273     u_int32_t reg20 = 0x0000e132;
1274     u_int32_t reg24 = 0x00017771;
1275
1276     switch (mode) {
1277     case ATA_PIO0:      reg20 = 0x0000e132; break;
1278     case ATA_PIO1:      reg20 = 0x00018121; break;
1279     case ATA_PIO2:      reg20 = 0x00024020; break;
1280     case ATA_PIO3:      reg20 = 0x00032010; break;
1281     case ATA_PIO4:      reg20 = 0x00040010; break;
1282     case ATA_WDMA2:     reg24 = 0x00002020; break;
1283     case ATA_UDMA2:     reg24 = 0x00911030; break;
1284     }
1285     ATA_OUTL(ch->r_bmio, (devno << 3) + 0x20, reg20);
1286     ATA_OUTL(ch->r_bmio, (devno << 3) + 0x24, reg24);
1287 }
1288
1289 static void
1290 promise_timing(struct ata_channel *ch, int devno, int mode)
1291 {
1292     u_int32_t timing = 0;
1293     struct promise_timing {
1294         u_int8_t  pa:4;
1295         u_int8_t  prefetch:1;
1296         u_int8_t  iordy:1;
1297         u_int8_t  errdy:1;
1298         u_int8_t  syncin:1;
1299         u_int8_t  pb:5;
1300         u_int8_t  mb:3;
1301         u_int8_t  mc:4;
1302         u_int8_t  dmaw:1;
1303         u_int8_t  dmar:1;
1304         u_int8_t  iordyp:1;
1305         u_int8_t  dmarqp:1;
1306         u_int8_t  reserved:8;
1307     } *t = (struct promise_timing*)&timing;
1308
1309     t->iordy = 1; t->iordyp = 1;
1310     if (mode >= ATA_DMA) {
1311         t->prefetch = 1; t->errdy = 1; t->syncin = 1;
1312     }
1313
1314     switch (ch->chiptype) {
1315     case 0x4d33105a:  /* Promise Ultra/Fasttrak 33 */
1316         switch (mode) {
1317         default:
1318         case ATA_PIO0:  t->pa =  9; t->pb = 19; t->mb = 7; t->mc = 15; break;
1319         case ATA_PIO1:  t->pa =  5; t->pb = 12; t->mb = 7; t->mc = 15; break;
1320         case ATA_PIO2:  t->pa =  3; t->pb =  8; t->mb = 7; t->mc = 15; break;
1321         case ATA_PIO3:  t->pa =  2; t->pb =  6; t->mb = 7; t->mc = 15; break;
1322         case ATA_PIO4:  t->pa =  1; t->pb =  4; t->mb = 7; t->mc = 15; break;
1323         case ATA_WDMA2: t->pa =  3; t->pb =  7; t->mb = 3; t->mc =  3; break;
1324         case ATA_UDMA2: t->pa =  3; t->pb =  7; t->mb = 1; t->mc =  1; break;
1325         }
1326         break;
1327
1328     case 0x4d38105a:  /* Promise Ultra/Fasttrak 66 */
1329     case 0x4d30105a:  /* Promise Ultra/Fasttrak 100 */
1330     case 0x0d30105a:  /* Promise OEM ATA 100 */
1331         switch (mode) {
1332         default:
1333         case ATA_PIO0:  t->pa = 15; t->pb = 31; t->mb = 7; t->mc = 15; break;
1334         case ATA_PIO1:  t->pa = 10; t->pb = 24; t->mb = 7; t->mc = 15; break;
1335         case ATA_PIO2:  t->pa =  6; t->pb = 16; t->mb = 7; t->mc = 15; break;
1336         case ATA_PIO3:  t->pa =  4; t->pb = 12; t->mb = 7; t->mc = 15; break;
1337         case ATA_PIO4:  t->pa =  2; t->pb =  8; t->mb = 7; t->mc = 15; break;
1338         case ATA_WDMA2: t->pa =  6; t->pb = 14; t->mb = 6; t->mc =  6; break;
1339         case ATA_UDMA2: t->pa =  6; t->pb = 14; t->mb = 2; t->mc =  2; break;
1340         case ATA_UDMA4: t->pa =  3; t->pb =  7; t->mb = 1; t->mc =  1; break;
1341         case ATA_UDMA5: t->pa =  3; t->pb =  7; t->mb = 1; t->mc =  1; break;
1342         }
1343         break;
1344     }
1345     pci_write_config(device_get_parent(ch->dev), 0x60 + (devno<<2), timing, 4);
1346 }
1347
1348 static void
1349 hpt_timing(struct ata_channel *ch, int devno, int mode)
1350 {
1351     device_t parent = device_get_parent(ch->dev);
1352     u_int32_t timing;
1353
1354     if (ch->chiptype == 0x00081103 && pci_get_revid(parent) >= 0x07) {
1355         switch (mode) {                                         /* HPT374 */
1356         case ATA_PIO0:  timing = 0x0ac1f48a; break;
1357         case ATA_PIO1:  timing = 0x0ac1f465; break;
1358         case ATA_PIO2:  timing = 0x0a81f454; break;
1359         case ATA_PIO3:  timing = 0x0a81f443; break;
1360         case ATA_PIO4:  timing = 0x0a81f442; break;
1361         case ATA_WDMA2: timing = 0x22808242; break;
1362         case ATA_UDMA2: timing = 0x120c8242; break;
1363         case ATA_UDMA4: timing = 0x12ac8242; break;
1364         case ATA_UDMA5: timing = 0x12848242; break;
1365         case ATA_UDMA6: timing = 0x12808242; break;
1366         default:        timing = 0x0d029d5e;
1367         }
1368     }
1369     else if ((ch->chiptype == 0x00041103 && pci_get_revid(parent) >= 0x05) ||
1370              (ch->chiptype == 0x00051103 && pci_get_revid(parent) >= 0x01)) {
1371         switch (mode) {                                         /* HPT372 */
1372         case ATA_PIO0:  timing = 0x0d029d5e; break;
1373         case ATA_PIO1:  timing = 0x0d029d26; break;
1374         case ATA_PIO2:  timing = 0x0c829ca6; break;
1375         case ATA_PIO3:  timing = 0x0c829c84; break;
1376         case ATA_PIO4:  timing = 0x0c829c62; break;
1377         case ATA_WDMA2: timing = 0x2c829262; break;
1378         case ATA_UDMA2: timing = 0x1c91dc62; break;
1379         case ATA_UDMA4: timing = 0x1c8ddc62; break;
1380         case ATA_UDMA5: timing = 0x1c6ddc62; break;
1381         case ATA_UDMA6: timing = 0x1c81dc62; break;
1382         default:        timing = 0x0d029d5e;
1383         }
1384     }
1385     else if (ch->chiptype == 0x00041103 && pci_get_revid(parent) >= 0x03) {
1386         switch (mode) {                                         /* HPT370 */
1387         case ATA_PIO0:  timing = 0x06914e57; break;
1388         case ATA_PIO1:  timing = 0x06914e43; break;
1389         case ATA_PIO2:  timing = 0x06514e33; break;
1390         case ATA_PIO3:  timing = 0x06514e22; break;
1391         case ATA_PIO4:  timing = 0x06514e21; break;
1392         case ATA_WDMA2: timing = 0x26514e21; break;
1393         case ATA_UDMA2: timing = 0x16494e31; break;
1394         case ATA_UDMA4: timing = 0x16454e31; break;
1395         case ATA_UDMA5: timing = 0x16454e31; break;
1396         default:        timing = 0x06514e57;
1397         }
1398         pci_write_config(parent, 0x40 + (devno << 2) , timing, 4);
1399     }
1400     else {                                                      /* HPT36[68] */
1401         switch (pci_read_config(parent, 0x41 + (devno << 2), 1)) {
1402         case 0x85:      /* 25Mhz */
1403             switch (mode) {
1404             case ATA_PIO0:      timing = 0x40d08585; break;
1405             case ATA_PIO1:      timing = 0x40d08572; break;
1406             case ATA_PIO2:      timing = 0x40ca8542; break;
1407             case ATA_PIO3:      timing = 0x40ca8532; break;
1408             case ATA_PIO4:      timing = 0x40ca8521; break;
1409             case ATA_WDMA2:     timing = 0x20ca8521; break;
1410             case ATA_UDMA2:     timing = 0x10cf8521; break;
1411             case ATA_UDMA4:     timing = 0x10c98521; break;
1412             default:            timing = 0x01208585;
1413             }
1414             break;
1415         default:
1416         case 0xa7:      /* 33MHz */
1417             switch (mode) {
1418             case ATA_PIO0:      timing = 0x40d0a7aa; break;
1419             case ATA_PIO1:      timing = 0x40d0a7a3; break;
1420             case ATA_PIO2:      timing = 0x40d0a753; break;
1421             case ATA_PIO3:      timing = 0x40c8a742; break;
1422             case ATA_PIO4:      timing = 0x40c8a731; break;
1423             case ATA_WDMA2:     timing = 0x20c8a731; break;
1424             case ATA_UDMA2:     timing = 0x10caa731; break;
1425             case ATA_UDMA4:     timing = 0x10c9a731; break;
1426             default:            timing = 0x0120a7a7;
1427             }
1428             break;
1429         case 0xd9:      /* 40Mhz */
1430             switch (mode) {
1431             case ATA_PIO0:      timing = 0x4018d9d9; break;
1432             case ATA_PIO1:      timing = 0x4010d9c7; break;
1433             case ATA_PIO2:      timing = 0x4010d997; break;
1434             case ATA_PIO3:      timing = 0x4010d974; break;
1435             case ATA_PIO4:      timing = 0x4008d963; break;
1436             case ATA_WDMA2:     timing = 0x2008d943; break;
1437             case ATA_UDMA2:     timing = 0x100bd943; break;
1438             case ATA_UDMA4:     timing = 0x100fd943; break;
1439             default:            timing = 0x0120d9d9;
1440             }
1441         }
1442     }
1443     pci_write_config(parent, 0x40 + (devno << 2) , timing, 4);
1444 }
1445
1446 static int
1447 hpt_cable80(struct ata_channel *ch)
1448 {
1449     device_t parent = device_get_parent(ch->dev);
1450     u_int8_t reg, val, res;
1451
1452     if (ch->chiptype == 0x00081103 && pci_get_function(parent) == 1) {
1453         reg = ch->unit ? 0x57 : 0x53;
1454         val = pci_read_config(parent, reg, 1);
1455         pci_write_config(parent, reg, val | 0x80, 1);
1456     }
1457     else {
1458         reg = 0x5b;
1459         val = pci_read_config(parent, reg, 1);
1460         pci_write_config(parent, reg, val & 0xfe, 1);
1461     }
1462     res = pci_read_config(parent, 0x5a, 1) & (ch->unit ? 0x01 : 0x02);
1463     pci_write_config(parent, reg, val, 1);
1464     return !res;
1465 }