Don't set prefetch mode on VIA chips, it causes problems on newer chips and
[dragonfly.git] / sys / dev / disk / ata / ata-pci.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-pci.c,v 1.32.2.15 2003/06/06 13:27:05 fjoe Exp $
29  * $DragonFly: src/sys/dev/disk/ata/ata-pci.c,v 1.14 2004/06/17 16:51:56 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/disk.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/buf.h>
39 #include <sys/malloc.h>
40 #include <sys/devicestat.h>
41 #include <sys/sysctl.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
45 #include <machine/clock.h>
46 #ifdef __alpha__
47 #include <machine/md_var.h>
48 #endif
49 #include <sys/rman.h>
50 #include <bus/pci/pcivar.h>
51 #include <bus/pci/pcireg.h>
52 #include "ata-all.h"
53
54 /* device structures */
55 struct ata_pci_controller {
56     struct resource *bmio;
57     int bmaddr;
58     struct resource *irq;
59     int irqcnt;
60 };
61
62 /* misc defines */
63 #define IOMASK                  0xfffffffc
64 #define GRANDPARENT(dev)        device_get_parent(device_get_parent(dev))
65 #define ATA_MASTERDEV(dev)      ((pci_get_progif(dev) & 0x80) && \
66                                  (pci_get_progif(dev) & 0x05) != 0x05)
67
68 int
69 ata_find_dev(device_t dev, u_int32_t devid, u_int32_t revid)
70 {
71     device_t *children;
72     int nchildren, i;
73
74     if (device_get_children(device_get_parent(dev), &children, &nchildren))
75         return 0;
76
77     for (i = 0; i < nchildren; i++) {
78         if (pci_get_devid(children[i]) == devid &&
79             pci_get_revid(children[i]) >= revid) {
80             free(children, M_TEMP);
81             return 1;
82         }
83     }
84     free(children, M_TEMP);
85     return 0;
86 }
87
88 static void
89 ata_via_southbridge_fixup(device_t dev)
90 {
91     device_t *children;
92     int nchildren, i;
93
94     if (device_get_children(device_get_parent(dev), &children, &nchildren))
95         return;
96
97     for (i = 0; i < nchildren; i++) {
98         if (pci_get_devid(children[i]) == 0x03051106 ||         /* VIA VT8363 */
99             pci_get_devid(children[i]) == 0x03911106 ||         /* VIA VT8371 */
100             pci_get_devid(children[i]) == 0x31021106 ||         /* VIA VT8662 */
101             pci_get_devid(children[i]) == 0x31121106) {         /* VIA VT8361 */
102             u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
103
104             if ((reg76 & 0xf0) != 0xd0) {
105                 device_printf(dev,
106                 "Correcting VIA config for southbridge data corruption bug\n");
107                 pci_write_config(children[i], 0x75, 0x80, 1);
108                 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
109             }
110             break;
111         }
112     }
113     free(children, M_TEMP);
114 }
115
116 static const char *
117 ata_pci_match(device_t dev)
118 {
119     if (pci_get_class(dev) != PCIC_STORAGE)
120         return NULL;
121
122     switch (pci_get_devid(dev)) {
123     /* supported chipsets */
124     case 0x12308086:
125         return "Intel PIIX ATA controller";
126
127     case 0x70108086:
128         return "Intel PIIX3 ATA controller";
129
130     case 0x71118086:
131     case 0x71998086:
132     case 0x84ca8086:
133         return "Intel PIIX4 ATA33 controller";
134
135     case 0x24218086:
136         return "Intel ICH0 ATA33 controller";
137
138     case 0x24118086:
139     case 0x76018086:
140         return "Intel ICH ATA66 controller";
141
142     case 0x244a8086:
143     case 0x244b8086:
144         return "Intel ICH2 ATA100 controller";
145
146     case 0x248a8086:
147     case 0x248b8086:
148         return "Intel ICH3 ATA100 controller";
149
150     case 0x24ca8086:
151     case 0x24cb8086:
152         return "Intel ICH4 ATA100 controller";
153
154     case 0x24d18086:
155         return "Intel ICH5 SATA150 controller";
156
157     case 0x24db8086:
158         return "Intel ICH5 ATA100 controller";
159
160     case 0x522910b9:
161         if (pci_get_revid(dev) >= 0xc4)
162             return "AcerLabs Aladdin ATA100 controller";
163         else if (pci_get_revid(dev) >= 0xc2)
164             return "AcerLabs Aladdin ATA66 controller";
165         else if (pci_get_revid(dev) >= 0x20)
166             return "AcerLabs Aladdin ATA33 controller";
167         else
168             return "AcerLabs Aladdin ATA controller";
169
170     case 0x05711106: 
171         if (ata_find_dev(dev, 0x05861106, 0x02))
172             return "VIA 82C586 ATA33 controller";
173         if (ata_find_dev(dev, 0x05861106, 0))
174             return "VIA 82C586 ATA controller";
175         if (ata_find_dev(dev, 0x05961106, 0x12))
176             return "VIA 82C596 ATA66 controller";
177         if (ata_find_dev(dev, 0x05961106, 0))
178             return "VIA 82C596 ATA33 controller";
179         if (ata_find_dev(dev, 0x06861106, 0x40))
180             return "VIA 82C686 ATA100 controller";
181         if (ata_find_dev(dev, 0x06861106, 0x10))
182             return "VIA 82C686 ATA66 controller";
183         if (ata_find_dev(dev, 0x06861106, 0))
184             return "VIA 82C686 ATA33 controller";
185         if (ata_find_dev(dev, 0x82311106, 0))
186             return "VIA 8231 ATA100 controller";
187         if (ata_find_dev(dev, 0x30741106, 0) ||
188             ata_find_dev(dev, 0x31091106, 0))
189             return "VIA 8233 ATA100 controller";
190         if (ata_find_dev(dev, 0x31471106, 0))
191             return "VIA 8233 ATA133 controller";
192         if (ata_find_dev(dev, 0x31771106, 0))
193             return "VIA 8235 ATA133 controller";
194         if (ata_find_dev(dev, 0x31491106, 0))
195             return "VIA 8237 ATA133 controller";
196         return "VIA Apollo ATA controller";
197
198     case 0x31491106:
199          return "VIA 8237 SATA 150 controller";
200
201     case 0x55131039:
202         if (ata_find_dev(dev, 0x07461039, 0))
203             return "SiS 5591 ATA133 controller";
204         if (ata_find_dev(dev, 0x06301039, 0x30) ||
205             ata_find_dev(dev, 0x06331039, 0) ||
206             ata_find_dev(dev, 0x06351039, 0) ||
207             ata_find_dev(dev, 0x06401039, 0) ||
208             ata_find_dev(dev, 0x06451039, 0) ||
209             ata_find_dev(dev, 0x06461039, 0) ||
210             ata_find_dev(dev, 0x06481039, 0) ||
211             ata_find_dev(dev, 0x06501039, 0) ||
212             ata_find_dev(dev, 0x07301039, 0) ||
213             ata_find_dev(dev, 0x07331039, 0) ||
214             ata_find_dev(dev, 0x07351039, 0) ||
215             ata_find_dev(dev, 0x07401039, 0) ||
216             ata_find_dev(dev, 0x07451039, 0) ||
217             ata_find_dev(dev, 0x07501039, 0))
218             return "SiS 5591 ATA100 controller";
219         else if (ata_find_dev(dev, 0x05301039, 0) ||
220             ata_find_dev(dev, 0x05401039, 0) ||
221             ata_find_dev(dev, 0x06201039, 0) ||
222             ata_find_dev(dev, 0x06301039, 0))
223             return "SiS 5591 ATA66 controller";
224         else
225             return "SiS 5591 ATA33 controller";
226
227     case 0x35121095:
228         return "SiI 3512 SATA controller";
229
230     case 0x31141095:
231         return "SiI 3114 SATA controller";
232
233     case 0x06801095:
234         return "SiI 0680 ATA133 controller";
235
236     case 0x06491095:
237         return "CMD 649 ATA100 controller";
238
239     case 0x06481095:
240         return "CMD 648 ATA66 controller";
241
242     case 0x06461095:
243         return "CMD 646 ATA controller";
244
245     case 0xc6931080:
246         if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
247             return "Cypress 82C693 ATA controller";
248         return NULL;
249
250     case 0x01021078:
251         return "Cyrix 5530 ATA33 controller";
252
253     case 0x74091022:
254         return "AMD 756 ATA66 controller";
255
256     case 0x74111022:
257         return "AMD 766 ATA100 controller";
258
259     case 0x74411022:
260         return "AMD 768 ATA100 controller";
261
262     case 0x74691022:
263         return "AMD 8111 UltraATA/133 controller";
264
265     case 0x01bc10de:
266         return "nVIDIA nForce ATA100 controller";
267
268     case 0x006510de:
269         return "nVIDIA nForce ATA133 controller";
270
271     case 0x00d510de:
272         return "nVIDIA nForce2 ATA133 controller";
273
274     case 0x02111166:
275         return "ServerWorks ROSB4 ATA33 controller";
276
277     case 0x02121166:
278         if (pci_get_revid(dev) >= 0x92)
279             return "ServerWorks CSB5 ATA100 controller";
280         else
281             return "ServerWorks CSB5 ATA66 controller";
282
283     case 0x02131166:
284         return "ServerWorks CSB6 ATA100 controller (channel 0+1)";
285
286     case 0x02171166:
287         return "ServerWorks CSB6 ATA66 controller (channel 2)";
288
289     case 0x4d33105a:
290         return "Promise ATA33 controller";
291
292     case 0x0d38105a:
293     case 0x4d38105a:
294         return "Promise ATA66 controller";
295
296     case 0x0d30105a:
297     case 0x4d30105a:
298         return "Promise ATA100 controller";
299
300     case 0x4d68105a:
301     case 0x6268105a: 
302         if (pci_get_devid(GRANDPARENT(dev)) == 0x00221011 &&
303             pci_get_class(GRANDPARENT(dev)) == PCIC_BRIDGE) {
304             static long start = 0, end = 0;
305
306             /* we belive we are on a TX4, now do our (simple) magic */
307             if (pci_get_slot(dev) == 1) {
308                 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
309                 return "Promise TX4 ATA100 controller (channel 0+1)";
310             }
311             else if (pci_get_slot(dev) == 2 && start && end) {
312                 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
313                 start = end = 0;
314                 return "Promise TX4 ATA100 controller (channel 2+3)";
315             }
316             else
317                 start = end = 0;
318         }
319         return "Promise TX2 ATA100 controller";
320
321     case 0x4d69105a:
322     case 0x5275105a:
323     case 0x6269105a: 
324     case 0x7275105a: 
325         return "Promise TX2 ATA133 controller";
326
327     case 0x00041103:
328         switch (pci_get_revid(dev)) {
329         case 0x00:
330         case 0x01:
331             return "HighPoint HPT366 ATA66 controller";
332         case 0x02:
333             return "HighPoint HPT368 ATA66 controller";
334         case 0x03:
335         case 0x04:
336             return "HighPoint HPT370 ATA100 controller";
337         case 0x05:
338             return "HighPoint HPT372 ATA133 controller";
339         }
340         return NULL;
341
342     case 0x00051103:
343         switch (pci_get_revid(dev)) {
344         case 0x01:
345         case 0x02:
346             return "HighPoint HPT372 ATA133 controller";
347         }
348         return NULL;
349
350     case 0x00081103:
351         switch (pci_get_revid(dev)) {
352         case 0x07:
353             return "HighPoint HPT374 ATA133 controller";
354         }
355         return NULL;
356
357     case 0x000116ca:
358         return "Cenatek Rocket Drive controller";
359
360    /* unsupported but known chipsets, generic DMA only */
361     case 0x10001042:
362     case 0x10011042:
363         return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
364
365     case 0x06401095:
366         return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
367
368     /* unknown chipsets, try generic DMA if it seems possible */
369     default:
370         if (pci_get_class(dev) == PCIC_STORAGE &&
371             (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
372             return "Generic PCI ATA controller";
373     }
374     return NULL;
375 }
376
377 static int
378 ata_pci_probe(device_t dev)
379 {
380     const char *desc = ata_pci_match(dev);
381     
382     if (desc) {
383         device_set_desc(dev, desc);
384         return 0;
385     } 
386     else
387         return ENXIO;
388 }
389
390 static int
391 ata_pci_add_child(device_t dev, int unit)
392 {
393     device_t child;
394
395     /* check if this is located at one of the std addresses */
396     if (ATA_MASTERDEV(dev)) {
397         if (!(child = device_add_child(dev, "ata", unit)))
398             return ENOMEM;
399     }
400     else {
401         if (!(child = device_add_child(dev, "ata", 2)))
402             return ENOMEM;
403     }
404     return 0;
405 }
406
407 static int
408 ata_pci_attach(device_t dev)
409 {
410     struct ata_pci_controller *controller = device_get_softc(dev);
411     u_int8_t class, subclass;
412     u_int32_t type, cmd;
413     int rid;
414
415     /* set up vendor-specific stuff */
416     type = pci_get_devid(dev);
417     class = pci_get_class(dev);
418     subclass = pci_get_subclass(dev);
419     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
420
421     if (!(cmd & PCIM_CMD_PORTEN)) {
422         device_printf(dev, "ATA channel disabled by BIOS\n");
423         return 0;
424     }
425
426     /* is busmastering supported ? */
427     if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 
428         (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
429
430         /* is there a valid port range to connect to ? */
431         rid = 0x20;
432         controller->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
433                                               0, ~0, 1, RF_ACTIVE);
434         if (!controller->bmio)
435             device_printf(dev, "Busmastering DMA not configured\n");
436     }
437     else
438         device_printf(dev, "Busmastering DMA not supported\n");
439
440     /* do extra chipset specific setups */
441     switch (type) {
442     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
443         pci_write_config(dev, 0x53, 
444                          (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
445         break;
446
447     case 0x4d38105a: /* Promise 66 & 100 (before TX2) need the clock changed */
448     case 0x4d30105a:
449     case 0x0d30105a:
450         ATA_OUTB(controller->bmio, 0x11, ATA_INB(controller->bmio, 0x11)|0x0a);
451         /* FALLTHROUGH */
452
453     case 0x4d33105a: /* Promise (before TX2) need burst mode turned on */
454         ATA_OUTB(controller->bmio, 0x1f, ATA_INB(controller->bmio, 0x1f)|0x01);
455         break;
456
457     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
458         if (pci_get_revid(dev) < 2) {   /* HPT 366 */
459             /* turn off interrupt prediction */
460             pci_write_config(dev, 0x51, 
461                              (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
462             break;
463         }
464         if (pci_get_revid(dev) < 5) {   /* HPT368/370 */
465             /* turn off interrupt prediction */
466             pci_write_config(dev, 0x51,
467                              (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
468             pci_write_config(dev, 0x55,
469                              (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
470
471             /* turn on interrupts */
472             pci_write_config(dev, 0x5a,
473                              (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
474
475             /* set clocks etc */
476             pci_write_config(dev, 0x5b, 0x22, 1);
477             break;
478         }
479         /* FALLTHROUGH */
480
481     case 0x00051103:    /* HighPoint HPT372 */
482     case 0x00081103:    /* HighPoint HPT374 */
483         /* turn off interrupt prediction */
484         pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
485         pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
486
487         /* turn on interrupts */
488         pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
489
490         /* set clocks etc */
491         pci_write_config(dev, 0x5b,
492                          (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
493         break;
494
495     case 0x05711106: /* VIA 82C586, '596, '686 default setup */
496         /* prepare for ATA-66 on the 82C686a and 82C596b */
497         if ((ata_find_dev(dev, 0x06861106, 0x10) && 
498              !ata_find_dev(dev, 0x06861106, 0x40)) || 
499             ata_find_dev(dev, 0x05961106, 0x12))
500             pci_write_config(dev, 0x50, 0x030b030b, 4);   
501
502         /* the southbridge might need the data corruption fix */
503         if (ata_find_dev(dev, 0x06861106, 0x40) ||
504             ata_find_dev(dev, 0x82311106, 0x10))
505             ata_via_southbridge_fixup(dev);
506         /* FALLTHROUGH */
507
508     case 0x74091022: /* AMD 756 default setup */
509     case 0x74111022: /* AMD 766 default setup */
510     case 0x74411022: /* AMD 768 default setup */
511     case 0x746d1022: /* AMD 8111 default setup */
512     case 0x01bc10de: /* NVIDIA nForce default setup */
513     case 0x006510de: /* NVIDIA nForce2 default setup */
514         /*
515          * set prefetch, postwrite.  Note: do not set this mode on VIA
516          * chipsets, it causes problems on newer chips and ATAPI devices.
517          */
518         if ((type & 0xFFFF) != 0x1106) {
519                 pci_write_config(dev, 0x41, 
520                                 pci_read_config(dev, 0x41, 1) | 0xf0, 1);
521         }
522
523         /* set fifo configuration half'n'half */
524         pci_write_config(dev, 0x43, 
525                          (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
526
527         /* set status register read retry */
528         pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
529
530         /* set DMA read & end-of-sector fifo flush */
531         pci_write_config(dev, 0x46, 
532                          (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
533
534         /* set sector size */
535         pci_write_config(dev, 0x60, DEV_BSIZE, 2);
536         pci_write_config(dev, 0x68, DEV_BSIZE, 2);
537         break;
538
539     case 0x02111166: /* ServerWorks ROSB4 enable UDMA33 */
540         pci_write_config(dev, 0x64,   
541                          (pci_read_config(dev, 0x64, 4) & ~0x00002000) |
542                          0x00004000, 4);
543         break;
544         
545     case 0x02121166: /* ServerWorks CSB5 enable UDMA66/100 depending on rev */
546         pci_write_config(dev, 0x5a,   
547                          (pci_read_config(dev, 0x5a, 1) & ~0x40) |
548                          (pci_get_revid(dev) >= 0x92) ? 0x03 : 0x02, 1);
549         break;
550
551     case 0x06801095: /* SiI 0680 set ATA reference clock speed */
552         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
553             pci_write_config(dev, 0x8a, 
554                              (pci_read_config(dev, 0x8a, 1) & 0x0F) | 0x10, 1);
555         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
556             device_printf(dev, "SiI 0680 could not set clock\n");
557         break;
558
559     case 0x06491095:
560     case 0x06481095:
561     case 0x06461095: /* CMD 646 enable interrupts, set DMA read mode */
562         pci_write_config(dev, 0x71, 0x01, 1);
563         break;
564
565     case 0x10001042:   /* RZ 100? known bad, no DMA */
566     case 0x10011042:
567     case 0x06401095:   /* CMD 640 known bad, no DMA */
568         controller->bmio = NULL;
569         device_printf(dev, "Busmastering DMA disabled\n");
570     }
571
572     if (controller->bmio) {
573         controller->bmaddr = rman_get_start(controller->bmio);
574         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
575                              SYS_RES_IOPORT, rid, controller->bmio);
576         controller->bmio = NULL;
577     }
578
579     /*
580      * the Cypress chip is a mess, it contains two ATA functions, but 
581      * both channels are visible on the first one.
582      * simply ignore the second function for now, as the right
583      * solution (ignoring the second channel on the first function)
584      * doesn't work with the crappy ATA interrupt setup on the alpha.
585      */
586     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
587         return 0;
588
589     ata_pci_add_child(dev, 0);
590
591     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
592         ata_pci_add_child(dev, 1);
593
594     return bus_generic_attach(dev);
595 }
596
597 static int
598 ata_pci_intr(struct ata_channel *ch)
599 {
600     u_int8_t dmastat;
601
602     /* 
603      * since we might share the IRQ with another device, and in some
604      * cases with our twin channel, we only want to process interrupts
605      * that we know this channel generated.
606      */
607     switch (ch->chiptype) {
608     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
609     case 0x00051103:    /* HighPoint HPT372 */
610     case 0x00081103:    /* HighPoint HPT374 */
611         if (((dmastat = ata_dmastatus(ch)) &
612             (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
613             return 1;
614         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
615         DELAY(1);
616         return 0;
617
618     case 0x06481095:    /* CMD 648 */
619     case 0x06491095:    /* CMD 649 */
620         if (!(pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
621               (ch->unit ? 0x08 : 0x04)))
622             return 1;
623 #if !defined(NO_ATANG)
624         pci_write_config(device_get_parent(ch->dev), 0x71,
625                 pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
626                 ~(ch->unit ? 0x04 : 0x08), 1);
627         break;
628 #endif
629
630     case 0x06801095:   /* SiI 680 */
631         if (!(pci_read_config(device_get_parent(ch->dev),
632                                 (ch->unit ? 0xb1 : 0xa1), 1) & 0x08))
633             return 1;
634         break;
635
636     case 0x4d33105a:    /* Promise Ultra/Fasttrak 33 */
637     case 0x0d38105a:    /* Promise Fasttrak 66 */
638     case 0x4d38105a:    /* Promise Ultra/Fasttrak 66 */
639     case 0x0d30105a:    /* Promise OEM ATA100 */
640     case 0x4d30105a:    /* Promise Ultra/Fasttrak 100 */
641         if (!(ATA_INL(ch->r_bmio, (ch->unit ? 0x14 : 0x1c)) &
642               (ch->unit ? 0x00004000 : 0x00000400)))
643             return 1;
644         break;
645
646     case 0x4d68105a:    /* Promise TX2 ATA100 */
647     case 0x6268105a:    /* Promise TX2 ATA100 */
648     case 0x4d69105a:    /* Promise TX2 ATA133 */
649     case 0x5275105a:    /* Promise TX2 ATA133 */
650     case 0x6269105a:    /* Promise TX2 ATA133 */
651     case 0x7275105a:    /* Promise TX2 ATA133 */
652         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
653         if (!(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x20))
654             return 1;
655         break;
656
657     case 0x24d18086:   /* Intel ICH5 SATA150 */
658         dmastat = ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT);
659         if ((dmastat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
660                 ATA_BMSTAT_INTERRUPT)
661             return 1;
662         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat &
663             ~(ATA_BMSTAT_DMA_SIMPLEX | ATA_BMSTAT_ERROR));
664         DELAY(1);
665         return 0;
666
667     }
668
669     if (ch->flags & ATA_DMA_ACTIVE) {
670         if (!((dmastat = ata_dmastatus(ch)) & ATA_BMSTAT_INTERRUPT))
671             return 1;
672         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
673         DELAY(1);
674     }
675     return 0;
676 }
677
678 static int
679 ata_pci_print_child(device_t dev, device_t child)
680 {
681     struct ata_channel *ch = device_get_softc(child);
682     int retval = 0;
683
684     retval += bus_print_child_header(dev, child);
685     retval += printf(": at 0x%lx", rman_get_start(ch->r_io));
686
687     if (ATA_MASTERDEV(dev))
688         retval += printf(" irq %d", 14 + ch->unit);
689     
690     retval += bus_print_child_footer(dev, child);
691
692     return retval;
693 }
694
695 static struct resource *
696 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
697                        u_long start, u_long end, u_long count, u_int flags)
698 {
699     struct ata_pci_controller *controller = device_get_softc(dev);
700     struct resource *res = NULL;
701     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
702     int myrid;
703
704     if (type == SYS_RES_IOPORT) {
705         switch (*rid) {
706         case ATA_IOADDR_RID:
707             if (ATA_MASTERDEV(dev)) {
708                 myrid = 0;
709                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
710                 end = start + ATA_IOSIZE - 1;
711                 count = ATA_IOSIZE;
712                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
713                                          SYS_RES_IOPORT, &myrid,
714                                          start, end, count, flags);
715             }
716             else {
717                 myrid = 0x10 + 8 * unit;
718                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
719                                          SYS_RES_IOPORT, &myrid,
720                                          start, end, count, flags);
721             }
722             break;
723
724         case ATA_ALTADDR_RID:
725             if (ATA_MASTERDEV(dev)) {
726                 myrid = 0;
727                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
728                 end = start + ATA_ALTIOSIZE - 1;
729                 count = ATA_ALTIOSIZE;
730                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
731                                          SYS_RES_IOPORT, &myrid,
732                                          start, end, count, flags);
733             }
734             else {
735                 myrid = 0x14 + 8 * unit;
736                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
737                                          SYS_RES_IOPORT, &myrid,
738                                          start, end, count, flags);
739                 if (res) {
740                         start = rman_get_start(res) + 2;
741                         end = start + ATA_ALTIOSIZE - 1;
742                         count = ATA_ALTIOSIZE;
743                         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
744                                              SYS_RES_IOPORT, myrid, res);
745                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
746                                                  SYS_RES_IOPORT, &myrid,
747                                                  start, end, count, flags);
748                 }
749             }
750             break;
751
752         case ATA_BMADDR_RID:
753             if (controller->bmaddr) {
754                 myrid = 0x20;
755                 start = (unit == 0 ? 
756                          controller->bmaddr : controller->bmaddr+ATA_BMIOSIZE);
757                 end = start + ATA_BMIOSIZE - 1;
758                 count = ATA_BMIOSIZE;
759                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
760                                          SYS_RES_IOPORT, &myrid,
761                                          start, end, count, flags);
762             }
763         }
764         return res;
765     }
766
767     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
768         if (ATA_MASTERDEV(dev)) {
769 #ifdef __alpha__
770             return alpha_platform_alloc_ide_intr(unit);
771 #else
772             int irq = (unit == 0 ? 14 : 15);
773
774             return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
775                                       SYS_RES_IRQ, rid, irq, irq, 1, flags);
776 #endif
777         }
778         else {
779             /* primary and secondary channels share interrupt, keep track */
780             if (!controller->irq)
781                 controller->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), 
782                                                      dev, SYS_RES_IRQ,
783                                                      rid, 0, ~0, 1, flags);
784             controller->irqcnt++;
785             return controller->irq;
786         }
787     }
788     return 0;
789 }
790
791 static int
792 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
793                          struct resource *r)
794 {
795     struct ata_pci_controller *controller = device_get_softc(dev);
796     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
797
798     if (type == SYS_RES_IOPORT) {
799         switch (rid) {
800         case ATA_IOADDR_RID:
801             if (ATA_MASTERDEV(dev))
802                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
803                                             SYS_RES_IOPORT, 0x0, r);
804             else
805                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
806                                             SYS_RES_IOPORT, 0x10 + 8 * unit, r);
807             break;
808
809         case ATA_ALTADDR_RID:
810             if (ATA_MASTERDEV(dev))
811                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
812                                             SYS_RES_IOPORT, 0x0, r);
813             else
814                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
815                                             SYS_RES_IOPORT, 0x14 + 8 * unit, r);
816             break;
817
818         case ATA_BMADDR_RID:
819             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
820                                         SYS_RES_IOPORT, 0x20, r);
821         default:
822             return ENOENT;
823         }
824     }
825     if (type == SYS_RES_IRQ) {
826         if (rid != ATA_IRQ_RID)
827             return ENOENT;
828
829         if (ATA_MASTERDEV(dev)) {
830 #ifdef __alpha__
831             return alpha_platform_release_ide_intr(unit, r);
832 #else
833             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
834                                         SYS_RES_IRQ, rid, r);
835 #endif
836         }
837         else {
838             /* primary and secondary channels share interrupt, keep track */
839             if (--controller->irqcnt)
840                 return 0;
841             controller->irq = NULL;
842             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
843                                         SYS_RES_IRQ, rid, r);
844         }
845     }
846     return EINVAL;
847 }
848
849 static int
850 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
851                    int flags, driver_intr_t *intr, void *arg,
852                    void **cookiep)
853 {
854     if (ATA_MASTERDEV(dev)) {
855 #ifdef __alpha__
856         return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
857 #else
858         return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
859                               flags, intr, arg, cookiep);
860 #endif
861     }
862     else
863         return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
864                               flags, intr, arg, cookiep);
865 }
866
867 static int
868 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
869                       void *cookie)
870 {
871     if (ATA_MASTERDEV(dev)) {
872 #ifdef __alpha__
873         return alpha_platform_teardown_ide_intr(irq, cookie);
874 #else
875         return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
876 #endif
877     }
878     else
879         return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
880 }
881
882 static device_method_t ata_pci_methods[] = {
883     /* device interface */
884     DEVMETHOD(device_probe,             ata_pci_probe),
885     DEVMETHOD(device_attach,            ata_pci_attach),
886     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
887     DEVMETHOD(device_suspend,           bus_generic_suspend),
888     DEVMETHOD(device_resume,            bus_generic_resume),
889
890     /* bus methods */
891     DEVMETHOD(bus_print_child,          ata_pci_print_child),
892     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
893     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
894     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
895     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
896     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
897     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
898     { 0, 0 }
899 };
900
901 static driver_t ata_pci_driver = {
902     "atapci",
903     ata_pci_methods,
904     sizeof(struct ata_pci_controller),
905 };
906
907 static devclass_t ata_pci_devclass;
908
909 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
910
911 static int
912 ata_pcisub_probe(device_t dev)
913 {
914     struct ata_channel *ch = device_get_softc(dev);
915     device_t *children;
916     int count, i;
917
918     /* find channel number on this controller */
919     device_get_children(device_get_parent(dev), &children, &count);
920     for (i = 0; i < count; i++) {
921         if (children[i] == dev)
922             ch->unit = i;
923     }
924     free(children, M_TEMP);
925     ch->chiptype = pci_get_devid(device_get_parent(dev));
926     ch->intr_func = ata_pci_intr;
927     return ata_probe(dev);
928 }
929
930 static device_method_t ata_pcisub_methods[] = {
931     /* device interface */
932     DEVMETHOD(device_probe,     ata_pcisub_probe),
933     DEVMETHOD(device_attach,    ata_attach),
934     DEVMETHOD(device_detach,    ata_detach),
935     DEVMETHOD(device_resume,    ata_resume),
936     { 0, 0 }
937 };
938
939 static driver_t ata_pcisub_driver = {
940     "ata",
941     ata_pcisub_methods,
942     sizeof(struct ata_channel),
943 };
944
945 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);